<?php
/** 检票月报后端处理 本页面只是处理参数 然后发送到s.12301.cc的pft/order_report/month.php进行查询
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-8-27
 * Time: 下午2:20
 */

/* 接受到js发来的参数
 * @param btime 开始时间
 * @param etime 结束时间
 * @param select_text 输入的文本
 * @param select_id   如果是点击下拉框 那么会有id 则不用上面传过来的文本
 * @param com_type  array 企业类型
 * */
require_once("/var/www/html/new/conf/le.je");
$le=new go_sql();
$le->connect();
$tmall_mark = $curr_mark = false;
$aid_str = $html = '';
$total = array();
$return = array(
    'total'=>array(
        'torder'=>0,
        'tnum'=>0,
        'money'=>0,
    ),
    'html'=>''
);
//echo '<pre>';
//print_r($_POST);
//echo '</pre>';exit;
$btime_post = date('Y-m-d',strtotime($_POST['time'].'01'));
$m = date('m',strtotime($_POST['time'].'01'));
switch ($m) {
	case '01':
    case '03':
    case '05':
    case '07':
    case '08':
    case '10':
    case '12':
        $etime_post = date('Y-m-d',strtotime($_POST['time'].'31'));
        break;
    case '04':
    case '06':
    case '09':
    case '11':
        $etime_post = date('Y-m-d',strtotime($_POST['time'].'30'));
        break;
    case '02':
        $etime_post = date('Y-m-d',strtotime($_POST['time'].'28'));
        break;
}
//处理企业类型
if(is_array($_POST['com_type']))
foreach($_POST['com_type'] as $v){
    switch ($v) {
        case '0'://全部
            $aid_post = '';
            $curr_mark= true;
            break 2;
    	case '1'://电商
            $aid_post .='1214,1454,1472,3034,2175,';
            $curr_mark= true;
    		/*OTA：
    		携程：100089 1214
            同程：200572  1454
            驴妈妈：200588 1472
            途牛：200974  3034
            去哪儿：123492  2175*/
    		break;
        case '2'://团购
            $aid_post .='1853,284,2706,';
            $curr_mark= true;
            /*团购：
            美团：200750 1853
            拉手：200044 284
            百度糯米：123545  2706*/
            break;
        case '3'://淘宝/天猫
            $tmall_mark = true;
            break;
        case '4'://景区
            $comtype_search .='"景区",';
            $curr_mark= true;
            break;
        case '5':
            $comtype_search .='"旅行社",';
            $curr_mark= true;
            break;
        case '6':
            $comtype_search .='"酒店",';
            $curr_mark= true;
            break;
        case '7':
            $comtype_search .='"其他",';
            $curr_mark= true;
            break;
    }
}else $curr_mark= true;

if(($comtype_search = rtrim($comtype_search,','))!='' && $_POST['select_id']==''){
    $sql = "select fid from pft_member_extinfo where com_type in ($comtype_search)";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $aid_post .= $row['fid'].',';
    }
}


if($_POST['select_id']!=''){
    $aid_post = $_POST['select_id'];
}

if($tmall_mark){
    $soap = new SoapClient(null,array(
            "location" => "http://localhost/open/openService/pft_insideMX.php",
            "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
    );
    $xml = simplexml_load_string($soap->Order_Globle_Search('','','','','','','','','','',$btime_post,$etime_post,'','淘宝码商','','','','',1,1,0,9999,0,0,'',0, 0, 0,'','','',0,0,0));
    if(count($xml)>1){
        foreach($xml->Rec as $v){
            if((string)$v->UUaids!='0'){
                $aids = (string)$v->UUaids;
                $v->UUtprice = (string)$v->UUaids_price.','.(string)$v->UUtprice;
            }else{
                $aids = (string)$v->UUaid;
            }
            $aid_str .= $aids.',';
            $aids = explode(',',$aids);
            $UUtprice = explode(',', (string)$v->UUtprice);
            $n=0;
            foreach($aids as $aid){
                $money = $UUtprice[$n]*(int)$v->UUtnum;
                ++$r[(int)((string)$v->UUlid.$aid)]['torder'];
                $r[(int)((string)$v->UUlid.$aid)]['tnum']  += (int)$v->UUtnum;
                $r[(int)((string)$v->UUlid.$aid)]['money'] += $money;
                $r[(int)((string)$v->UUlid.$aid)]['ltitle'] = (string)$v->UUltitle;
                $r[(int)((string)$v->UUlid.$aid)]['aid']    = (int)$aid;
                ++$n;
                ++$total['torder'];
                $total['tnum'] += (int)$v->UUtnum;
                $total['money']+=$money/100;
            }
        }
    }
}
if($curr_mark == true){
    $query = array(
        'aid'=>$aid_post,
        'btime'=>$btime_post,
        'etime'=>$etime_post,

    );
    $return = curr($query);
//    echo '<pre>';
//    print_r($return);
//    echo '</pre>';
//    exit;
}

if($aid_str!=''){
    $aid_str = rtrim($aid_str,',');
    $query = array('ty'=>'aid','aid'=>$aid_str);
    $dnames = curr_1($query);
    $n = $return['n'] ? $return['n']+1 : 0;
    foreach($r as $v){
        $fff = $n%2==0?'#FFFFFF':'#F7F7F7';
        $html .= <<<HTML
<tr style="background:{$fff}">
    <td class="setAlign setTd">{$dnames[$v['aid']]['com_type']}</td>
    <td class="setAlign setTd">{$dnames[$v['aid']]['dname']}</td>
    <td class="setAlign setTd">{$v['ltitle']}</td>
    <td>{$v['torder']}</td>
    <td>{$v['tnum']}</td>
    <td class="setColor_a">&yen{$v['money']}</td>
</tr>
HTML;
        ++$n;
    }
}


//exit;
$html = $return['html'].$html;
$total['torder']+= $return['total']['torder'];
$total['tnum']  += $return['total']['tnum'];
$total['money'] += $return['total']['money'];
echo json_encode(array('total'=>$total,'html'=>$html));
exit;
function curr($query){
    $query['token'] = md5($query['btime'].'7667659191929397103470329788807862402627565924638280479623896');
    $url = "http://s.12301.cc/pft/order_report/month.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//    return curl_exec($ch);
    return json_decode(curl_exec($ch),true);
}

function curr_1($query){
    $query['time'] = $_SERVER['REQUEST_TIME'];
    $query['token'] = md5($query['time'].'0393156885586815305050516493889730425518949732797083380078766300585');
    $url = "http://www.12301.cc/module/zax/gmm.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//    return curl_exec($ch);
    return json_decode(curl_exec($ch),true);
}